package com.hundsun.yr.universal.p2p.ui.activity;

import android.net.http.SslError;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.base.BaseAppManager;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseMineWebActivity;
import com.hundsun.yr.universal.p2p.utils.Logs;

public class WebAutoBidSignActivity extends BaseMineWebActivity {
    /**
     * 支持放大缩小
     */
    public static final String supportZoom = "supportZoom";
    private boolean supportZoomFlag;

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    protected View getLoadingTargetView() {

        if (mBrowserLayout == null) {
        }
        return mBrowserLayout;
    }


    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {
        mBrowserLayout.refresh();
        toggleHide();
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        supportZoomFlag = extras.getBoolean(supportZoom);
    }

    @Override
    protected void onNetworkDisConnected() {

        toggleNetworkError(true, new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if (NetUtils.isNetworkConnected(getBaseContext())) {
                    showToast("请检查网络...");
                } else {
                    mBrowserLayout.refresh();
                    toggleHide();
                }
            }
        });

    }


    @Override
    protected void initViewsAndEvents() {
        super.initViewsAndEvents();
        toolbar_title.setText(mWebTitle);
        if (supportZoomFlag) {
            mBrowserLayout.mWebView.getSettings().setSupportZoom(supportZoomFlag);
            mBrowserLayout.mWebView.getSettings().setBuiltInZoomControls(supportZoomFlag);
        }
        mBrowserLayout.mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Logs.e("888url: " + url);
                if (url.contains(getString(R.string.invest_jiankong2))) {
                    if (BaseAppManager.getInstance().getActivity(AutoBidSignActiviy.class) != null) {
                        BaseAppManager.getInstance().getActivity(AutoBidSignActiviy.class).finish();
                    }

                    finish();
                }  else if (url.contains(getString(R.string.login_successful2))){
                    //跳转至交易密码设置界面
                    readyGoThenKill(UpdatePayPwdActivity.class);
                } else {
                    mBrowserLayout.mWebView.loadUrl(url);
                }
                return true;
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

        });


//		mToolBar.setNavigationOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				webBack();
//			}
//		});
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        // TODO Auto-generated method stub
        return false;
    }

    /**
     * 网页 返回
     */
    public void webBack() {
//        if (mBrowserLayout.canGoBack()) {
//            mBrowserLayout.goBack();
//        } else {
//            finish();
//        }
        finish();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        if (keyCode == KeyEvent.KEYCODE_BACK) {

            webBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
